use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use salvo::oapi::ToSchema;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "users")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = true)]
    pub id: i64,
    pub username: String,
    pub password: String,
    pub avatar: String,
    pub introduction: String,
    pub create_by: String,
    #[sea_orm(column_type = "Timestamp")]
    pub create_time: chrono::NaiveDateTime,
    pub update_by: String,
    #[sea_orm(column_type = "Timestamp")]
    pub update_time: chrono::NaiveDateTime,
    pub tenant_id: i64,
    pub deleted: i32,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize,ToSchema)]
pub struct SafeUser {
    pub tenant_id: i64,
    pub id: Option<i64>,
    pub username: Option<String>,
    pub avatar: Option<String>,
    pub introduction: Option<String>,
    pub create_by: Option<String>,
    pub create_time: Option<chrono::NaiveDateTime>,
    pub update_by: Option<String>,
    pub update_time: Option<chrono::NaiveDateTime>,
}

impl From<Model> for SafeUser {
    fn from(model: Model) -> Self {
        Self {
            tenant_id: model.tenant_id,
            id: Some(model.id),
            username: Some(model.username),
            avatar: Some(model.avatar),
            introduction: Some(model.introduction),
            create_by: Some(model.create_by),
            create_time: Some(model.create_time),
            update_by: Some(model.update_by),
            update_time: Some(model.update_time),
        }
    }
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
